<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MSlug;
use App\Models\MSlugClassify;
use Illuminate\Http\Request;

/**
 * 信息管理控制器
 */
class SlugController extends Controller
{
    /**
     * 碎片分类列表界面
     */
    public function slugClassifyPage()
    {
        $this->data['classifies'] = MSlugClassify::all();
        return view("admin.slug_classifies")->with($this->data);
    }

    /**
     * 碎片分类编辑界面
     * @param $id
     * @return $this
     */
    public function slugClassifyEditPage($id)
    {
        $this->data['classify'] = MSlugClassify::find($id);
        $this->data['classifies'] = MSlugClassify::where('id', '!=', $id)->get();
        return view("admin.slug_classify_edit")->with($this->data);
    }

    /**
     * 碎片分类信息存储
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function slugClassifySave(Request $request, $id)
    {
        if (!$this->validateReq($request, MSlugClassify::editRulesWithMsg(0), MSlugClassify::editRulesWithMsg(1))) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return response()->json($this->data);
        }
        $classify = MSlugClassify::find($id);
        $parent = MSlugClassify::find($request->parent);
        if (empty($classify)) {
            $classify = new MSlugClassify();
        }

        if ($classify->saveData($parent, $request->slug, $request->title, $request->intro)) {
            $this->data = $this->makeJsCode(0, "保存成功！");
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }

        return response()->json($this->data);
    }

    /**
     * 碎片分类删除
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function slugClassifyDel($id)
    {
        //如果分类下还有文章，则不可以删除
        if (MSlug::where('cid', $id)->count() > 0) { //
            $this->data = $this->makeJsCode(1, "此分类下还有碎片未删除！");
        } else {
            MSlugClassify::destroy($id);
            $this->data = $this->makeJsCode(0, "删除成功！");
        }
        return back()->with($this->data);
    }

    /**
     * 碎片列表界面
     * @param Request $request
     * @param $cid  分类ID
     * @param int $index
     * @return $this
     */
    public function slugPage(Request $request, $cid, $index = 1)
    {
        //开启分页搜索支持
        $this->fyInit(route("admin.slug", ['cid' => $cid]), 20, $request->getQueryString());
        $query = (new MSlug())->query();

        //按时间过滤
        if ($request->has(['start', 'end'])) {
            $query = $query->where('time', '>=', strtotime($request->start))
                ->where('time', '<=', strtotime($request->end));
        }

        //按标题搜索
        if ($request->has('search_str')) {
            $query = $query->where('title', 'like', "%" . $request->search_str . "%");
        }

        //排序
        if ($request->has('sort_type')) {
            if ($request->sort_type == 1) {//按修改时间降序
                $query = $query->orderBy("time", "desc");
            } elseif ($request->sort_type == 2) {//按修改时间升序
                $query = $query->orderBy("time", "asc");
            }
        }

        $this->fyCalc($query->where('cid', $cid)->count(), $index);
        $this->data['news'] = $query->where('cid', $cid)->skip($this->fy_pgOffset)->take($this->fy_pgSize)->orderBy('time', 'desc')->get();
        $this->data['classifies'] = MSlugClassify::all();
        $this->data['menu_selected'] = MSlugClassify::find($cid);
        $this->fyViewInit($this->data);
        return view("admin.slug")->with($this->data);
    }

    /**
     * 碎片编辑界面
     */
    public function slugEditPage($id, $cid)
    {
        $this->data['slug'] = MSlug::find($id);
        $this->data['classifies'] = MSlugClassify::all();
        $this->data['menu_selected'] = MSlugClassify::find($cid);
        return view("admin.slug_edit")->with($this->data);
    }

    /**
     * 碎片内容存储
     */
    public function slugSave(Request $request, $id)
    {
        if (!$this->validateReq($request, MSlug::editRulesWithMsg(0), MSlug::editRulesWithMsg(1))) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return response()->json($this->data);
        }

        //检查文章分类
        $classify = MSlugClassify::find($request->cid);
        if (empty($classify)) {
            $this->data = $this->makeJsCode(1, "请必须选择碎片分类！");
            return response()->json($this->data);
        }

        $slug = MSlug::find($id);
        if (empty($slug)) {
            $slug = new MSlug();
        }

        if ($slug->saveData($classify, $request)) {
            $this->data = $this->makeJsCode(0, "保存成功！");
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }

        return response()->json($this->data);
    }

    /**
     * 碎片删除处理
     */
    public function slugDel($id)
    {
        $img = MSlug::find($id)->img;

        if (MSlug::destroy($id)) {
            //删除图片
            if (!empty($img)) {
                unlink(public_path() . '/' . $img);
            }
        }
        $this->data = $this->makeJsCode(0, "删除成功！");
        return back()->with($this->data);
    }
}